home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue25 / tiptrix / listing4.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-18  |  2.6 KB  |  120 lines

  1. unit xUpgrade;
  2.  { can be used to upgrade programs. The class should be used
  3.    at a very early stage in program run }
  4.  
  5. {
  6.   it uses an INI file to store the current version
  7.  
  8.   the TxUpgrader must be inherited and overwrite
  9.   the virtual Upgrade method in the following form :
  10.  
  11. function TMyUpgrader.Upgrade(aVersion: Integer): Integer;
  12. begin
  13.   Result := inherited Upgrade(aVersion);
  14.   if aVersion < 110 then
  15.      try
  16.        ... do the upgrade for Version 1.10
  17.        Result := 110;
  18.      except
  19.      end
  20.   else if aVersion < 120 then
  21.      try
  22.        ... do the upgrade for Verison 1.20
  23.        Result := 120;
  24.      except
  25.      end;
  26. end;
  27.  
  28.   To start the upgrade process simply call the "Execute" method.
  29.   This method call "Upgrade" until no different version number was
  30.   returned by the function.
  31.  
  32.   The following would initate a complete upgrade process :
  33.  
  34.   with TMyUpgrader.Create('DB') do
  35.     try
  36.       Execute;
  37.     finally
  38.       Free;
  39.     end;
  40. }
  41.  
  42. interface
  43.  
  44. uses
  45.   SysUtils, Classes, IniFiles;
  46.  
  47. type
  48.   TxUpgrader = class
  49.   private
  50.     fIniFile : TIniFile;
  51.     fSection : String;
  52.     procedure SetVersion(Value: Integer);
  53.     function  GetVersion: Integer;
  54.   public
  55.     constructor Create(const aSection: String);
  56.     destructor Destroy; override;
  57.  
  58.     procedure Execute;
  59.     function  Upgrade(aVersion: Integer): Integer; virtual;
  60.  
  61.     property  Version: Integer read GetVersion write SetVersion;
  62.   end;
  63.  
  64. implementation
  65.  
  66. const
  67.   INI_VERSION  = 'VERSION';
  68.  
  69. constructor TxUpgrader.Create(const aSection: String);
  70. begin
  71.   inherited Create;
  72.   fSection:=aSection;
  73.   fIniFile:=TIniFile.Create(ChangeFileExt(ParamStr(0),'.ini'));
  74. end;
  75.  
  76. destructor TxUpgrader.Destroy;
  77. begin
  78.   fIniFile.Free;
  79.   inherited Destroy;
  80. end;
  81.  
  82. procedure TxUpgrader.SetVersion(Value: Integer);
  83. begin
  84.   fIniFile.WriteInteger(fSection,INI_VERSION,Value);
  85.   fIniFile.WriteString(fSection,'V'+IntToStr(Value),DateTimeToStr(Now));
  86. end;
  87.  
  88. function  TxUpgrader.GetVersion: Integer;
  89. begin
  90.   Result := fIniFile.ReadInteger(fSection,INI_VERSION,100);
  91. end;
  92.  
  93. procedure TxUpgrader.Execute;
  94. var
  95.   oldVersion,
  96.   aVersion: Integer;
  97. begin
  98.   aVersion:=GetVersion;
  99.   while oldVersion<>aVersion do begin
  100.     oldVersion:=aVersion;
  101.     aVersion:=Upgrade(aVersion);
  102.     if aVersion<>oldVersion then
  103.        SetVersion(aVersion);
  104.   end;
  105. end;
  106.  
  107. function TxUpgrader.Upgrade(aVersion: Integer): Integer;
  108. begin
  109.   Result:=aVersion;
  110.   if aVersion < 110 then
  111.      try
  112.        Result := 110
  113.      except
  114.      end
  115.   else if aVersion < 120 then
  116.      Result := 120;
  117. end;
  118.  
  119. end.
  120.